package cn.ruituo.web;

import cn.ruituo.entity.Page;
import cn.ruituo.entity.Subject;
import cn.ruituo.service.SubjectService;
import cn.ruituo.util.StrUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "SubjectsServlet", value = "/subjectServlet")
public class SubjectsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = StrUtil.toInt(request.getParameter("pageNo"), 1);
        Page<Subject> page = SubjectService.service.page(pageNo, 10, request.getParameter("subjectname"));

        response.setContentType("application/json;charset=utf-8");
        //request.setAttribute("paging",page);
//        StringBuilder sb = new StringBuilder();
//        sb.append("{");
//        sb.append("\"pageNo\":" + page.getPageNo());
//        sb.append(",\"totalPages\":" + page.getTotalPages());
//        sb.append(",");
//        sb.append("\"datas\":[");
//        for (int i = 0; i < page.getDatas().size(); i++) {
//            Subject s = page.getDatas().get(i);
//            sb.append("{");
//            sb.append("\"id\":" + s.getId());
//            sb.append(",\"subjectname\":\"" + s.getSubjectname() + "\"");
//            sb.append(",\"classhour\":" + s.getClasshour());
//            sb.append(",\"gradeid\":" + s.getGradeid());
//            sb.append("}");
//            if (i < page.getDatas().size() - 1)
//                sb.append(",");
//
//        }
//        sb.append("]");
//        sb.append("}");
        String sb= JSON.toJSONString(page);
        PrintWriter writer = response.getWriter();
        writer.print(sb);
        writer.flush();
    }
}
